<?php

namespace App\Engine;

use Carbon\Carbon;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Illuminate\Support\Facades\Cache;
use Ramsey\Uuid\Uuid;

class BaseCommon
{
    function curl_get($url, &$httpCode = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //不做证书校验,部署在linux环境下请改为true
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt(
            $ch, CURLOPT_HTTPHEADER,
            array(
                'Content-Type: application/json',
            )
        );
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $file_contents = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $file_contents;
    }

    function curl_post($url, array $params = array())
    {
        $data_string = json_encode($params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt(
            $ch, CURLOPT_HTTPHEADER,
            array(
                'Content-Type: application/json',
            )
        );
        $data = curl_exec($ch);
        curl_close($ch);
        return ($data);
    }

    public function create_qrcode()
    {
        $phrase=new PhraseBuilder();
        $code=$phrase->build(4);
        $builder=new CaptchaBuilder($code,$phrase);
        $builder->setBackgroundColor(34,0,45);
        $builder->setMaxAngle(5 );
        $builder->setMaxBehindLines(10);
        $builder->setMaxFrontLines(10);
        $builder->setTextColor(230,81,175);
        $builder->build(150,40);
        $phrase=$builder->getPhrase();
        $client_id=Uuid::uuid1()->toString();
        Cache::put($client_id,$phrase,Carbon::now()->addMinute(2));
        $data=[
            'client'=>$client_id,
            'captcha'=>$builder->inline()
        ];
        return $data;
    }

    /**
     * 订单编号生成
     */
    public function create_order_num($type="C")
    {
        $orderSn =
           $type. strtoupper(dechex(date('m'))) . date(
                'd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(
                '%02d', rand(0, 99));
        return $orderSn;
    }






}
